package com.ktgj.customer.controller;

import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ktgj.customer.domain.Itinerary;
import com.ktgj.customer.domain.vo.ItineraryMiddleVo;
import com.ktgj.customer.domain.vo.ItineraryOrderVo;
import com.ktgj.customer.domain.vo.TmpItineraryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.common.security.annotation.PreAuthorize;
import com.ktgj.customer.domain.ItineraryMiddle;
import com.ktgj.customer.service.IItineraryMiddleService;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.core.utils.poi.ExcelUtil;
import com.ktgj.common.core.web.page.TableDataInfo;

/**
 * 行程定制业务中间Controller
 * 
 * @author ruoyi
 * @date 2021-09-01
 */
@ControllerAdvice
@RestController
@RequestMapping("/itineraryMiddle")
public class ItineraryMiddleController extends BaseController
{
    @Autowired
    private IItineraryMiddleService itineraryMiddleService;

    /**
     * 查询行程定制业务中间列表
     */
    @PreAuthorize(hasPermi = "customer:itineraryMiddle:list")
    @GetMapping("/list")
    public TableDataInfo list(ItineraryMiddle itineraryMiddle)
    {
        startPage();
        List<ItineraryMiddle> list = itineraryMiddleService.selectItineraryMiddleList(itineraryMiddle);
        return getDataTable(list);
    }

    /**
     * 导出行程定制业务中间列表
     */
    @PreAuthorize(hasPermi = "customer:itineraryMiddle:export")
    @Log(title = "行程定制业务中间", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ItineraryMiddle itineraryMiddle) throws IOException
    {
        List<ItineraryMiddle> list = itineraryMiddleService.selectItineraryMiddleList(itineraryMiddle);
        ExcelUtil<ItineraryMiddle> util = new ExcelUtil<ItineraryMiddle>(ItineraryMiddle.class);
        util.exportExcel(response, list, "行程定制业务中间数据");
    }

    /**
     * 获取行程定制业务中间详细信息
     */
    @PreAuthorize(hasPermi = "customer:itineraryMiddle:query")
    @GetMapping(value = "/{primaryKeyId}")
    public AjaxResult getInfo(@PathVariable("primaryKeyId") Long primaryKeyId)
    {
        return AjaxResult.success(itineraryMiddleService.selectItineraryMiddleById(primaryKeyId));
    }

    /**
     * 新增行程定制业务中间
     */
    @PreAuthorize(hasPermi = "customer:itineraryMiddle:add")
    @Log(title = "行程定制业务中间", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ItineraryMiddle itineraryMiddle)
    {
        return toAjax(itineraryMiddleService.insertItineraryMiddle(itineraryMiddle));
    }

    /**
     * 修改行程定制业务中间
     */
    @PreAuthorize(hasPermi = "customer:itineraryMiddle:edit")
    @Log(title = "行程定制业务中间", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ItineraryMiddle itineraryMiddle)
    {
        return toAjax(itineraryMiddleService.updateItineraryMiddle(itineraryMiddle));
    }

    /**
     * 删除行程定制业务中间
     */
    @PreAuthorize(hasPermi = "customer:itineraryMiddle:remove")
    @Log(title = "行程定制业务中间", businessType = BusinessType.DELETE)
	@DeleteMapping("/{primaryKeyIds}")
    public AjaxResult remove(@PathVariable Long[] primaryKeyIds)
    {
        return toAjax(itineraryMiddleService.deleteItineraryMiddleByIds(primaryKeyIds));
    }

    /**
     * 订单加入行程
     * */
    @PostMapping("/addItineraryMiddle")
    public AjaxResult addItineraryMiddle(@RequestBody ItineraryMiddleVo itineraryMiddleVo) {
        if (itineraryMiddleVo != null){
            int i;
            try {
                i = itineraryMiddleService.addItineraryMiddle(itineraryMiddleVo);
            } catch (Exception e){
                return AjaxResult.error(e.getMessage());
            }
            if (i > 0){
                return AjaxResult.success("添加成功");
            }
            else {
                return AjaxResult.error("添加失败");
            }
        }
        else {
            return AjaxResult.error("缺少必要参数");
        }
    }

    /**
     * 查看行程
     * */
    @PostMapping("/selecItineraryMiddle")
    public AjaxResult selecItineraryMiddle(@RequestBody Map<String, Object> map) {
        if (map.get("itineraryCustomizeId") == null || map.get("itineraryCustomizeId").equals("")){
            return AjaxResult.error("缺少必要参数");
        }
        Long itineraryCustomizeId = Long.valueOf(map.get("itineraryCustomizeId").toString());
        ItineraryOrderVo itineraryOrderVo;
        try {
            itineraryOrderVo = itineraryMiddleService.selecItineraryMiddle(itineraryCustomizeId);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(itineraryOrderVo);
    }

    /**
     * 移除行程中的订单
     * */
    @PostMapping("/delItineraryMiddle")
    public AjaxResult delItineraryMiddle(@RequestBody Map<String, Object> map) {
        if (map.get("primaryKeyId") != null && !map.get("primaryKeyId").equals("")){
            Long primaryKeyId = Long.valueOf(map.get("primaryKeyId").toString());
            int i = itineraryMiddleService.delItineraryMiddle(primaryKeyId);
            if (i > 0){
                return AjaxResult.success("移除成功");
            }
            else {
                return AjaxResult.error("移除失败");
            }
        }
        else {
            return AjaxResult.error("缺少必要参数");
        }
    }

    /**
     * 行程列表
     * */
    @PostMapping("/journeyList")
    public AjaxResult journeyList(HttpServletRequest request){
        TmpItineraryVo tmpItineraryVo;
        try {
            tmpItineraryVo = itineraryMiddleService.journeyList(request);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

        return AjaxResult.success(tmpItineraryVo);
    }
}
